package com.zxl.yjb.utils;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.zxl.yjb.R;
import com.zxl.yjb.app.AppManager;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class PickerViewUtils {



    /**
     * 时间选择器
     * @param listener
     */
    public static void timePickerView(OnTimeSelectListener listener, boolean[] booleen, String... strings){
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        if(strings.length>0){
            startDate.set(1980,1,1);
            endDate.set(endDate.get(Calendar.YEAR),endDate.get(Calendar.MONTH),endDate.get(Calendar.DAY_OF_MONTH));
        }else{
            startDate.set(startDate.get(Calendar.YEAR),startDate.get(Calendar.MONTH),startDate.get(Calendar.DAY_OF_MONTH));
            endDate.set(2050,1,1);
        }
        TimePickerView pvTime = new TimePickerBuilder(AppManager.getInstance().currentActivity(), listener)
                .setType(booleen)
                .setCancelText("取消")
                .setSubmitText("确定")
                .setContentTextSize(18)
                .setOutSideCancelable(true)
                .isCyclic(true)//是否循环滚动
                .setSubmitColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.red_color))
                .setCancelColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.color_9B9B9B))
                .setTitleBgColor(0xFFffffff)
                .setBgColor(0xFFffffff)
//                .setDate(new Date())// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate,endDate)
                .setLabel("年","月","日","时","分","秒")
                .isCenterLabel(false)
                .isDialog(false)
                .build();
        pvTime.show();
    }
    /**
     * 时间选择器
     * @param listener
     */
    public static void timePickerViewWithDate( OnTimeSelectListener listener,boolean[] booleen,String... strings){
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
       /* if(strings.length>0){
            startDate.set(1951,1,1);
            endDate.set(endDate.get(Calendar.YEAR),endDate.get(Calendar.MONTH),endDate.get(Calendar.DAY_OF_MONTH));
        }else{
            startDate.set(startDate.get(Calendar.YEAR),startDate.get(Calendar.MONTH),startDate.get(Calendar.DAY_OF_MONTH));
            endDate.set(2050,1,1);
        }*/
        TimePickerView pvTime = new TimePickerBuilder(AppManager.getInstance().currentActivity(), listener)
                .setType(booleen)
                .setCancelText("取消")
                .setSubmitText("确定")
                .setContentTextSize(18)
                .setOutSideCancelable(true)
                .isCyclic(true)
                .setSubmitColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.red_color))
                .setCancelColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.color_9B9B9B))
                .setTitleBgColor(0xFFffffff)
                .setBgColor(0xFFffffff)
//                        .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
//                .setRangDate(startDate,endDate)//起始终止年月日设定
                .setRangDate(startDate,endDate)
                .setLabel("年","月","日","时","分","秒")
                .isCenterLabel(false)
                .isDialog(false)
                .build();
        pvTime.show();
    }


    private static OptionsPickerView pvCustomOptions;

    /**
     * 自定义条目选择器
     * @param cardItem
     * @param selectListener
     */
    public static void customOptionPickerView(final List<String> cardItem,OnOptionsSelectListener selectListener){
        pvCustomOptions = new OptionsPickerBuilder(AppManager.getInstance().currentActivity(),selectListener)
                .setLayoutRes(R.layout.pickerview_custom_option, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        final TextView tvCancel = (TextView) v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();
                            }
                        });

                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.dismiss();
                            }
                        });
                    }
                })
                .isDialog(false)
                .build();
        pvCustomOptions.setPicker(cardItem);//添加数据
        pvCustomOptions.show();
    }


    public static void OptionPickerView(final List<String> cardItem,OnOptionsSelectListener selectListener){
        pvCustomOptions = new OptionsPickerBuilder(AppManager.getInstance().currentActivity(),selectListener)
                .setLayoutRes(R.layout.pickerview_custom_option, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        final TextView tvCancel = (TextView) v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();
                            }
                        });

                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.dismiss();
                            }
                        });
                    }
                })
                .isDialog(false)
                .build();
        pvCustomOptions.setPicker(cardItem);
        pvCustomOptions.show();
    }



    /**
     * 时间选择器
     * @param listener
     */
    public static void timePickerViewWithRange( OnTimeSelectListener listener,int startTime,int endTime,boolean[] booleen){

        SimpleDateFormat sdf= new SimpleDateFormat("yyyy");
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        try {
            String startTimeStr = startTime+"";
            Date startdate= sdf.parse(startTimeStr);
            startCalendar.setTime(startdate);

            String endTimeStr = endTime+"";
            Date enddate= sdf.parse(endTimeStr);
            endCalendar.setTime(enddate);
        } catch (Exception e) {

        }

        TimePickerView pvTime = new TimePickerBuilder(AppManager.getInstance().currentActivity(), listener)
                .setType(booleen)
                .setCancelText("取消")
                .setSubmitText("确定")
                .setContentTextSize(18)
                .setOutSideCancelable(true)
                .isCyclic(true)
                .setSubmitColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.red_color))
                .setCancelColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.color_9B9B9B))
                .setTitleBgColor(0xFFffffff)
                .setBgColor(0xFFffffff)
//                .setDate(new Date())// 如果不设置的话，默认是系统时间*/
                .setRangDate(startCalendar,endCalendar)
                .setLabel("年","月","日","时","分","秒")
                .isCenterLabel(false)
                .isDialog(false)
                .build();
        pvTime.show();
    }


    /**
     * 时间选择器
     * @param listener
     */
    public static void timePickerViewWithRangeAndDate( OnTimeSelectListener listener,int startTime,int endTime,String nowDate,boolean[] booleen){
        Date date;
        if (!TextUtils.isEmpty(nowDate))
        {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                date = sdf.parse(nowDate);
            } catch (Exception e) {
                date = new Date();
            }
        }
        else
        {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        SimpleDateFormat sdf= new SimpleDateFormat("yyyy");

        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        try {
            String startTimeStr = startTime+"";
            Date startdate= sdf.parse(startTimeStr);
            startCalendar.setTime(startdate);

            String endTimeStr = endTime+"";
            Date enddate= sdf.parse(endTimeStr);
            endCalendar.setTime(enddate);
        } catch (Exception e) {

        }

        TimePickerView pvTime = new TimePickerBuilder(AppManager.getInstance().currentActivity(), listener)
                .setType(booleen)
                .setCancelText("取消")
                .setSubmitText("确定")
                .setContentTextSize(18)
                .setOutSideCancelable(true)
                .isCyclic(true)//是否循环滚动
                .setSubmitColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.red_color))
                .setCancelColor(AppManager.getInstance().currentActivity().getResources().getColor(R.color.color_9B9B9B))
                .setTitleBgColor(0xFFffffff)
                .setBgColor(0xFFffffff)
                .setDate(calendar)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startCalendar,endCalendar)
                .setLabel("年","月","日","时","分","秒")
                .isCenterLabel(false)
                .isDialog(false)
                .build();
        pvTime.show();
    }

}
